package com.zyn.es.basic;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class EsClient {

    public static final String INDEX = "blog";
    public static final String TYPE  = "article";
    public Logger              log   = LogManager.getLogger();
    public TransportClient     client;

    public TransportClient initClient() {
        Builder builder = Settings.builder();
        //builder.put("cluster.name", "myClusterName");
        //builder.put("client.transport.sniff", true);
        //ping节点的超时时间，默认5s
        //builder.put("client.transport.ping_timeout", 5);
        //How often to sample / ping the nodes listed and connected. Defaults to 5s.
        //builder.put("client.transport.nodes_sampler_interval", 5);
        client = new PreBuiltTransportClient(Settings.EMPTY);
        try {
            client.addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        //client.close();
        return client;
    }

    public void printResponse(DocWriteResponse response) {
        log.info("index：" + response.getIndex());
        log.info("type：" + response.getType());
        log.info("id：" + response.getId());
        log.info("version：" + response.getVersion());
        log.info("status：" + response.status());
        log.info("response：" + response);
    }
}
